﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Sedge.Core;
using Sedge.Core.Globalization;
using Sedge.Core.Utils;
using Sedge.UI.ViewModel;

namespace Sedge.UI.View
{
	public partial class MasterForm : Form
	{
		#region Private Fields
		
		public delegate void NavigationEventHandler();
		private event NavigationEventHandler NextViewEvent;

		private readonly MasterViewModel _viewModel;
		private Control _currentView;
		
		private bool _finish;

		#endregion //Private Fields

		#region Constructors

		public MasterForm()
		{
			InitializeComponent();
		}

		public MasterForm(MasterViewModel viewModel) 
			: this()
		{
			_viewModel = viewModel;
			NextViewEvent = ShowNextView;

			SetupWindow();
			ShowNextView();
		}
		
		#endregion Constructors

		#region Private Methods

		private void SetupWindow()
		{
			Services.ViewManager.LockingChanged += ViewManager_LockingChanged;
			buttonBack.Text = Locale.Strings.ButtonBack;
			buttonNext.Text = Locale.Strings.ButtonNext;
			buttonCancel.Text = Locale.Strings.ButtonCancel;

			Text = _viewModel.GetCaption();

			if (!String.IsNullOrEmpty(Locale.Banner))
			{
				try
				{
					Image image = Image.FromFile(Locale.Banner);
					Image background = new Bitmap(panelBanner.Width, panelBanner.Height);
					using (Graphics g = Graphics.FromImage(background))
					{
						g.DrawImage(image, background.Width - image.Width, background.Height - image.Height);
					}
					panelBanner.BackgroundImage = background;
				}
				catch (Exception)
				{
					ErrorHandler.Warning(Locale.Strings.EFileNotFound, Locale.Banner);
				}
			}

		}

		private void ViewManager_LockingChanged(bool isLocked)
		{
			SetupButtons();
		}
		
		private void SetupButtons()
		{
			if (!_viewModel.HasNextView())
			{
				buttonNext.Text = Locale.Strings.ButtonFinish;
				NextViewEvent = Finish;
				buttonCancel.Enabled = Services.ViewManager.IsLocked;
			}
			else
			{
				buttonNext.Text = Locale.Strings.ButtonNext;
				NextViewEvent = ShowNextView;
				buttonCancel.Enabled = true;
			}

			buttonBack.Enabled = _viewModel.HasPrevView() && !Services.ViewManager.IsLocked;
			buttonNext.Enabled = !Services.ViewManager.IsLocked;
		}

		#endregion //Private Methods

		#region Private Navigation Methods

		private void ShowPrevView()
		{
			ShowView(_viewModel.GetPrevView());
		}

		private void ShowNextView()
		{
			ShowView(_viewModel.GetNextView());
		}
		
		private void ShowView(Control view)
		{
			if (_currentView != null)
			{
				panelMain.Controls.Remove(_currentView);
			}

			_currentView = view;
			labelCaption.Text = _viewModel.GetViewCaption();
			if (_currentView != null)
			{
				panelMain.Controls.Add(_currentView);
				_currentView.Dock = DockStyle.Fill;
				_currentView.Show();
			}

			SetupButtons();
		}


		private void Finish()
		{
			if (Services.ViewManager.CleanOnExit)
				_viewModel.Clean();
			
			_finish = true;
			Close();
		}

		#endregion //Private Navigation Methods

		#region Event handlers

		private void MasterForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (_finish)
				return;

			if (e.CloseReason == CloseReason.UserClosing)
			{
				if (!Services.Communications.Question(Locale.Strings.MFormCancel, Locale.Strings.MFormCaption))
				{
					e.Cancel = true;
				}
			}

			if (!e.Cancel)
			{
				_viewModel.Cancel();
			}
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void buttonNext_Click(object sender, EventArgs e)
		{
			NextViewEvent();
		}

		private void buttonBack_Click(object sender, EventArgs e)
		{
			ShowPrevView();
		}

		#endregion Event handlers	

	}
}
